package ui.util;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;

public class FontUtil {
	private static String sp = "/";

	public static String getFileNameByPath(String path) {
		String s = path;
		if (path.contains("/")) {
			s = path.substring(path.lastIndexOf(sp) + 1, path.length());
		}
		return s;
	}

	public static String getFatherPath(String path) {
		String fatherPath = path;
		if (path.contains(sp)) {
			int e = path.lastIndexOf(sp);
			if (e == 0) {
				e = 1;
			}
			fatherPath = path.substring(0, e);
		}
		return fatherPath;
	}

	public static double getFontWidth(Graphics2D g2d, Font font, String text) {
		FontRenderContext context = g2d.getFontRenderContext();
		// 获取字体的像素范围对象
		Rectangle2D stringBounds = font.getStringBounds(text, context);
		double fontWidth = stringBounds.getWidth();
		return fontWidth;
	}

	public static double getFontHeight(Graphics2D g2d, Font font) {
		FontRenderContext context = g2d.getFontRenderContext();
		// 获取字体的像素范围对象
		Rectangle2D stringBounds = font.getStringBounds("w", context);
		double fontHeight = stringBounds.getHeight();
		return fontHeight;
	}
}
